<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="flex items-center justify-between">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="普通兑换" name="0"></el-tab-pane>
          <el-tab-pane label="奖励升级" name="1"></el-tab-pane>
          <el-tab-pane label="终极大奖" name="2"></el-tab-pane>
        </el-tabs>
        <div>
          <span>已使用碎片：{{count}}{{ activeName === '0' ? '个' : '套'}}，</span>
          <span>产出奖励价值：{{price}}m豆</span>
        </div>
      </div>

      <div class="flex justify-between mt-4 mb-4">
        <div class="flex justify-end items-center">
          <div class="flex items-center mr-4">
            <span class="item">用户编号：</span>
            <el-input v-model="listQuery.userCode" clearable placeholder="请输入用户编号" size="normal"></el-input>
          </div>
          <div class="flex items-center mr-4">
            <span class="item">兑换时间：</span>
            <el-date-picker
                v-model="listQuery.startTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择开始时间"
                class="mr-3"
              >
              </el-date-picker>
              <el-date-picker
                v-model="listQuery.endTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择结束时间"
              >
              </el-date-picker>
          </div>
          <el-button size="default" type="primary" @click="getTableData();getExchangeStatistics()">搜索</el-button>
          <el-button size="default" type="primary" @click="handleReset">重置</el-button>
        </div>
      </div>

      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        border
        class="mt-3"
        stripe
        style="width: 100%"
        tooltip-effect="dark"
      >
        <el-table-column :index="indexMethod" label="序号" prop="user_code" type="index" width="120px"></el-table-column>
        <el-table-column label="用户编号" prop="user_info.user_code"></el-table-column>
        <el-table-column label="用户昵称" prop="user_info.nickname"></el-table-column>
        <el-table-column label="使用碎片数量(个)" prop="chip_number"></el-table-column>
        <el-table-column label="获得奖励" prop="detail">
          <template slot-scope="{row}">
            <el-popover
              placement="left"
              width="400"
              trigger="hover">
                <div v-for="item in row.log" :key="item.id" class="flex mb-4 items-center">
                  <img :src="item.detail.picture" alt="" style="max-width: 80px;max-height: 80px">
                  <p class="ml-4">
                    {{item.detail.name}}
                  </p>
                  <p v-if="item.count > 1">
                    <span>数量：</span>{{ item.count }}
                  </p>
                  <p v-if="item.reward_type === 'product'">
                    <span>有效期：</span>{{ item.number }}天
                  </p>
                </div>
                <div class="popoverHeight"  slot="reference">
                  <div v-for="(item, index) in row.log" :key="item.id" class="flex mb-4">
                    <p v-if="index<3">
                      {{item.detail.name}}
                    </p>
                    <p v-if="item.count > 1 && index < 3">
                      <span>数量：</span>{{ item.count }}
                    </p>
                    <p v-if="item.reward_type === 'product' && index < 3">
                      <span>有效期：</span>{{ item.number }}天
                    </p>
                  </div>
                </div>
            </el-popover>
            <!-- <div v-for="item in row.log" :key="item.id" class="flex items-center">
              
              <el-popover placement="right" trigger="hover">
                <img :src="item.detail.picture" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" class="imageIcon" :src="item.detail.picture" fit="fill" :lazy="true"></img>
              </el-popover>
              <div>
                <p>
                  {{item.detail.name}}
                </p>
                <p v-if="item.count > 1">
                  <span>数量：</span>{{ item.count }}
                </p>
                <p v-if="item.reward_type === 'product'">
                  <span>有效期：</span>{{ item.number }}天
                </p>
              </div>
            </div> -->
          </template>
        </el-table-column>
        <el-table-column label="奖励价值" prop="price">
          <template slot-scope="{row}">
            {{ row.price > 0 ? row.price : '--' }}
          </template>
        </el-table-column>
        <el-table-column label="兑换时间" prop="created_at"></el-table-column>
      </el-table>

      <!-- 分页 -->
      <cus-pagination
        v-show="total > 0"
        :limit.sync="listQuery.perPage"
        :page.sync="listQuery.page"
        :total="total"
        @pagination="getTableData();getExchangeStatistics()"
      />
    </el-card>

  </div>
</template>

<script>
import { exchangeLog, exchangeStatistics } from '@/api/sunGame/sunGame'

export default {
  name: 'exchangeRecord',
  components: {},
  data() {
    return {
      activeName: '0',
      dialogVisible: false,
      dialogVisible2: false,
      formData: {
        chip_id: undefined,
        prize_id: undefined,
        count: undefined,
        is_open: undefined,
        sort: undefined,
        id: undefined
      },
      rules: {
        chip_id: [{ required: true, message: '请选择关联碎片', trigger: 'blur' }],
        prize_id: [{ required: true, message: '请选择奖励', trigger: 'blur' }],
        count: [{ required: true, message: '请输入锻造所需碎片数', trigger: 'blur' }],
      },
      loading: false,
      tableData: [],
      listQuery: {
        page: 1,
        perPage: 10,
        type: 0, //0=兑换 1=升级 2=终极
        userCode: undefined,
        startTime: undefined,
        endTime: undefined
      },
      total: 1,
      options: [],
      options2: [],
      fileList: [],
      dialogTitle: undefined,
      count: 0,
      price: 0
    }
  },
  created() {
    this.getExchangeStatistics()
    this.getTableData()
  },
  methods: {
    indexMethod(index) {
      return this.total - (this.listQuery.page - 1) * this.listQuery.perPage - index
    },
    handleClick(val) {
      this.listQuery.type = val.name
      this.listQuery.userCode = undefined
      this.getTableData()
      this.getExchangeStatistics()
    },
    // 网络请求
    /**
     * getTableData
     * @desc 获取表格数据
     */
    async getTableData() {
      this.loading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = !this.listQuery[key] ? undefined : this.listQuery[key]
      }
      if(!this.listQuery.type) {
        this.listQuery.type = 0
      }
      const { data, code } = await exchangeLog(this.listQuery)
      if (code === 200) {

        this.tableData = data.list
        this.total = data.meta.total
      }
      this.loading = false

    },
    getExchangeStatistics() {
      for (let key in this.listQuery) {
        this.listQuery[key] = !this.listQuery[key] ? undefined : this.listQuery[key]
      }
      if(!this.listQuery.type) {
        this.listQuery.type = 0
      }
      exchangeStatistics(this.listQuery).then(res => {
        if(res.code === 200) {
          this.count = res.data.count
          this.price = res.data.price
        }
      })
    },
    handleReset() {
      this.listQuery = {
        page: 1,
        perPage: 10,
        type: 0, //0=兑换 1=升级 2=终极
        userCode: undefined,
        startTime: undefined,
        endTime: undefined
      }
      this.getTableData()
      this.getExchangeStatistics()
    },
    
  }
}
</script>

<style lang="scss" scoped>
.infoBox {
  margin-top      : 15px;
  margin-bottom   : 15px;
  padding-bottom  : 15px;
  border          : 1px solid #999999;
  border-radius   : 10px;
  display         : flex;
  align-items     : baseline;
  justify-content : center;
  box-sizing      : border-box;
  padding-right   : 30px;

  .info_1 {
    display         : flex;
    justify-content : center;
    flex-direction  : column;
    width           : 100%;

    .info_2 {
      margin-top  : 15px;
      display     : flex;
      align-items : center;
      position    : relative;

      span {
        width      : 180px;
        text-align : right;
      }

      .tip {
        color     : red;
        font-size : 12px;
        position  : absolute;
        top       : 100%;
        left      : 120px;
      }
    }
  }
}

.item {
  width : 100px;
}

.formItemBox {
  display     : flex;
  align-items : center;
  flex-wrap   : wrap;

  width       : 100%;
  overflow    : hidden;
}

::v-deep {
  .formItemBox {
    .el-form-item {
      width : 50% !important;
    }
  }
  .el-table {
    .cell {
      display: block !important;
    }
  }
}
.imageIcon {
  width: 80px;
}
</style>

